#include "settings.h"

settings::settings(QWidget *parent) :
    QDialog(parent)
{
    //setGeometry(0, 0, 400, 150);
    setMaximumSize(400,150);
    setMinimumSize(400,150);
    this->setWindowTitle("Settings");
    WelcomeLabel = new QLabel;
    ContinuousShootingLabel = new QLabel;
    PcsLabel = new QLabel;
    VideoLabel = new QLabel;
    SecLabel = new QLabel;

    PhotoLineEdit = new QLineEdit;
    RecordLineEdit = new QLineEdit;

    SaveBtn = new QPushButton;
    ExitBtn = new QPushButton;

    WelcomeLabel->setText("SEETINGS");
    ContinuousShootingLabel->setText("Continuous Shooting");
    PcsLabel->setText("pcs\t(Default 1)");
    VideoLabel->setText("Record");
    SecLabel->setText("s\t(Default 3)");
    SaveBtn->setText("Save");
    ExitBtn->setText("Exit");

    layout1 = new QVBoxLayout;
    layout2 = new QVBoxLayout;
    layout3 = new QVBoxLayout;
    layout4 = new QHBoxLayout;
    layout5 = new QHBoxLayout;
    layout6 = new QVBoxLayout;

    layout1->addWidget(ContinuousShootingLabel);
    layout1->addWidget(VideoLabel);

    layout2->addWidget(PhotoLineEdit);
    layout2->addWidget(RecordLineEdit);

    layout3->addWidget(PcsLabel);
    layout3->addWidget(SecLabel);

    layout4->addLayout(layout1);
    layout4->addLayout(layout2);
    layout4->addLayout(layout3);

    layout5->addWidget(SaveBtn);
    layout5->addWidget(ExitBtn);

    layout6->addWidget(WelcomeLabel);
    layout6->addLayout(layout4);
    layout6->addLayout(layout5);

    this->setLayout(layout6);


    connect(ExitBtn,SIGNAL(clicked()),this,SLOT(close()));
    connect(SaveBtn, SIGNAL(clicked()),this,SLOT(saveParameter()));
}

settings::~settings()
{

}

bool settings::qtSqlInit(void)
{
    QSqlDatabase db1 = QSqlDatabase::addDatabase("QSQLITE");
    db1.setDatabaseName("qtsql");
    if(!db1.open())
    {
        qDebug()<<"connect sql failure!";
        return false;
    }

    QSqlDatabase db = QSqlDatabase::database("QSQLITE");

    QSqlQuery query(db);
    bool createStatus = query.exec("CREATE TABLE qtsql(qt_id INTEGER PRIMARY KEY, qt_setting VARCHAR, qt_pictures INTEGER, qt_videos INTEGER)");
    if(!createStatus)
    {
        qDebug()<<"create sql data failure or the sql has been created!";
        return false;
    }

    query.prepare("INSERT INTO qtsql(qt_id, qt_setting, qt_pictures, qt_videos) VALUES(:qt_id, :qt_setting, :qt_pictures, :qt_videos)");

    query.addBindValue(1);
    query.addBindValue("QtSetting");
    query.addBindValue(1);
    query.addBindValue(3);

    bool status = query.exec();
    if(!status)
    {
        qDebug()<<"qtSqlInit >> inster sql data failure!";
        return false;
    }
    return true;
}

bool settings::queryAll(void)
{
    QSqlDatabase db = QSqlDatabase::database("QSQLITE");
    QSqlQuery query(db);

    query.exec("select * from qtsql");

    QSqlRecord rec = query.record();
    qDebug() << QObject::tr("qtsql表字段数：" ) << rec.count();

    while(query.next())
    {
        for(int index = 0; index < rec.count(); index++)
            qDebug() << query.value(index) << " ";
        qDebug() << "\n";
    }
    return true;
}

bool settings::updateById(int pictures, int videos)
{
    int id = 1;

    QSqlDatabase db = QSqlDatabase::database("QSQLITE");
    QSqlQuery query(db);

    query.prepare(QString("update qtsql set qt_pictures=?, qt_videos=? where qt_id=%1").arg(id));

    query.bindValue(0,pictures);
    query.bindValue(1, videos);


    bool status = query.exec();
    if(!status)
    {
        qDebug()<<"update sql data failure!";
        db.close();
        return false;
    }
    db.close();
    return true;
}

int settings::queryPictures(void)
{
    QSqlDatabase db = QSqlDatabase::database("QSQLITE");
    QSqlQuery query(db);
    int pictures;

    query.exec("select * from qtsql");

    while (query.next())
    {
        pictures = query.value(2).toInt();
        qDebug()<<"the picture number is : " <<QString::number(pictures, 10);
    }
    return pictures;
}

int settings::queryVideos(void)
{
    QSqlDatabase db = QSqlDatabase::database("QSQLITE");
    QSqlQuery query(db);

    int videos;
    query.exec("select * from qtsql");

    while (query.next())
    {
        videos = query.value(3).toInt();
        qDebug()<<"the picture number is : " <<QString::number(videos, 10);
    }
    return videos;
}

void settings::saveParameter()
{
    /* get input photo numbers */
    QString QStPhoto = PhotoLineEdit->text();
    int photoNumber = pf.qstringToInt(QStPhoto);

     /* get input recording times  */
    QString QStRecord = RecordLineEdit->text();
    int recordTimes = pf.qstringToInt(QStRecord);

    /* update sql, write photo numbers, times to sql */
    bool status = updateById(photoNumber, recordTimes);
    if(!status)
    {
        qDebug()<<"setting >> saveParameter >> inster sql number failure";
        return;
    }
}
